import { QFrame_App } from "../../ttapi_layer2/ttlayer2.js";
import { UserData, UserState } from "../game.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js"
import { tt } from "../../ttapi_interface/ttapi.js";
import { State_MainMenu } from "./state_mainmenu.js";
import { GameConfig } from "../model/gameconfig.js";

export class State_Game extends UserState {
    constructor(configurl: string) {
        super();
        this._configurl = configurl;
    }
    override OnInit(statemgr: QFrame_App<UserData>): void {
        super.OnInit(statemgr);
        this.InitAsync();
    }
    override  OnExit(): void {
        let _ui = this.userdata.scene.GetUI();
        _ui.removeChildAll();
        let _root = this.userdata.scene.GetRootGroup();
        _root.RemoveChildAll();

    }
    private _inited: boolean = false;
    private _configurl: string;
    private _gameConfig: GameConfig;

    async InitAsync(): Promise<void> {


        //加载游戏配置
        this._gameConfig = new GameConfig();
        await this._gameConfig.LoadAsync(this._configurl);

        let _root = this.userdata.scene.GetRootGroup();
        //加载背景图
        {
            let back = this.userdata.scene.CreateSceneObj();

            _root.AddChild(back);
            let s = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync(this._gameConfig.battleground.tex);
            let coms = new tt2.QFrame_Comp_Sprite(s);
            coms.renderlayer = -1;
            back.AddComponent(coms);
            back.tran.X = -s.totalWidth / 2;
            back.tran.Y = - s.totalHeight / 2;
        }
        //加载一个动画
        {
            let basee = this.userdata.scene.CreateSceneObj();
            _root.AddChild(basee);
            let c = tt2.QAni_PlayerMgr.CreatePlayer("pbase");
            if (c == null) {
                await tt2.QAni_PlayerMgr.LoadPlayerInfo("piece/pack/pbase.ttani.json");
                c = tt2.QAni_PlayerMgr.CreatePlayer("pbase");
            }
            basee.AddComponent(new tt2.QFrame_Comp_QAni(c));

        }
        //配置UI
        let _ui = this.userdata.scene.GetUI();
        let _uiMidY = _ui.getWorldRect().Height / 2 - 20;
        {
            let l = new tt2.QUI_Label(this.app.font, "游戏模式");
            l.localRect.setVPosByTopBorder(20, _uiMidY - 120);
            _ui.addChild(l);
        }
        {
            let l = new tt2.QUI_Label(this.app.font, "这个模式还没开发完");
            l.localRect.setVPosByTopBorder(20, _uiMidY - 100);
            _ui.addChild(l);
        }



        {
            let _btn1 = await this.CreateBtn("退出", 1.0);
            _btn1.localRect.setVPosByTopBorder(20, 10);
            _btn1.localRect.setHPosByLeftBorder(60, 10);
            _ui.addChild(_btn1);
            _btn1.OnClick = () => {
                this.app.ChangeState(new State_MainMenu());
            }
        }


        this._inited = true;
    }
    async CreateBtn(text: string, fontscale: number = 2.0): Promise<tt2.QUI_Button> {
        let _btnborder = await tt2.QFrame_ResMgr.LoadStandaloneSpriteAsync("btnborder.png");
        let btn = new tt2.QUI_Button();
        btn.localRect.setHPosByCenter(160);
        btn.localRect.setVPosByBottomBorder(40, 30);
        let bimg: tt2.QUI_ImageScale9;
        {
            let s9 = new tt2.QUI_Scale9(_btnborder, new tt.Border(4, 4, 4, 4));
            let label = new tt2.QUI_Label(this.app.font, text);
            label.fontScale.X = fontscale;
            label.fontScale.Y = fontscale;
            label.color = new tt.Color(0.3, 0.3, 0.3, 1);
            bimg = new tt2.QUI_ImageScale9(s9);
            label.localRect.setAsFill();
            bimg.addChild(label);
            bimg.localRect.setAsFill();
        }
        let bimg2 = bimg.Clone() as tt2.QUI_ImageScale9;
        bimg2.color = new tt.Color(0.5, 0.5, 0.5, 1.0);

        //bimg2.localRect.setAsFill();
        btn.ElemNormal = bimg;
        btn.ElemPress = bimg2;
        return btn;
    }
}